<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Steve
 * Date: 17/08/12
 * Time: 17:32
 * To change this template use File | Settings | File Templates.
 */
namespace Lib\Forms;

use Lib\Forms\BaseForm;
use Exception;
use Lib\Forms\BaseElement;


class Form
{
    private $formHtml;
    private $formName = "";
    private $baseFormName;
    private $formMethod ="POST";
    private $formAction = "#";
    private $formElements = array();

    public function FormOpen()
    {
        $this->formHtml .= \"<form";
        $this->formHtml .= $this->formName;
        $this->formHtml .= $this->formMethod;
        $this->formHtml .= $this->formAction;
        $this->formHtml .= ">";
    }

    public function FormClose()
    {
        $this->formHtml .= "</form>";
    }

    public function AddElement(BaseElement $Element)
    {
        $this->formElements[] = $Element;
    }



    public function GetFormRender()
    {
        $this->FormOpen();

        foreach ($this->formElements as $formElement)
        {
            /** @var $formElement BaseElement */
             $this->formHtml .= $formElement->RenderElement();
            $this->formHtml .="\r\n";
        }

        $this->FormClose();

        return $this->formHtml;
    }

    public function setFormName($formName)
    {
        $this->formName = " name=".$formName. " ";
        $this->baseFormName = $formName;
    }

    public function getFormName()
    {
        return $this->baseFormName;
    }

    /**
     * @Desc Sets the form action
     * @param $action string The requested form action must be POST or GET.
     * @throws \Exception
     */
    public function setFormMethod($method)
    {

        $method = strtoupper($method);

        if(!$method == "POST" || !$method =="GET")
        {
            throw(new Exception('Form->Method can only contain POST or GET as valid values.You have passed '.$method.' as the form action.'));
        }
        else
        {
        $this->formMethod = " method=".$method." ";
        }
    }

    public function setFormAction($action)
    {
        //TODO: Default form action should be "#"
        $this->formAction = " action=".$action." ";
    }

    public function OpenFieldSet($name, $legend, $disabled = false)
    {
        $fieldSet = new Elements\FieldSetElement();
        $fieldSet->SetName($name);
        $fieldSet->SetLegend($legend);

        if($disabled === true)
        {
            $fieldSet->SetDisabled();
        }

        $fieldSet->OpenFieldSet();

        $this->AddElement($fieldSet);
    }

    public function CloseFieldSet()
    {
        $fieldSet = new Elements\FieldSetElement();
        $fieldSet->CloseFieldSet();

        $this->AddElement($fieldSet);
    }

    public function GetFormData($fieldName)
    {
        if(isset($POST['.$fieldName.']))
        {
            return $_POST['.$fieldName.'];
        }
        else
        {
            //TODO: Throw Error message Unknown field name requested from form.
            return;
        }
    }

}
